using System;
using System.Windows.Forms;
using System.Data.Common;
using System.ComponentModel;

namespace Tilver.DBConnectionUI.Editors
{
    [ToolboxItem(false)]
    internal partial class SqlCompactEditor : UserControl, IUDLEditor
	{
        SqlCeConnectionStringBuilder builder = new SqlCeConnectionStringBuilder();

        public SqlCompactEditor()
		{
			InitializeComponent();

            this.DataSource.TextChanged += delegate { builder.DataSource =  this.DataSource.Text; };
            this.Password.TextChanged += delegate { builder.Password = this.Password.Text; };
            this.ckPersistsecInfo.CheckedChanged += delegate { builder.PersistSecurityInfo = ckPersistsecInfo.Checked; };
        }

        private void cmdFindDB_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            fd.Filter = "Sql Server Mobile Edition Databases *.sdf|*.sdf|All Files *.*|*.*";
            if (fd.ShowDialog(this.Parent) == DialogResult.OK)
            {
                DataSource.Text = fd.FileName;
            }
        }

        #region IConnectionEditor Members

        public string ConnectionString
        {
            get
            {
                return builder.ConnectionString;
            }
            set
            {
                builder.ConnectionString = value;
                RefreshData();
            }
        }

        public void RefreshData()
        {
            this.DataSource.Text = builder.DataSource;
            this.Password.Text = builder.Password;
            this.ckPersistsecInfo.Checked = builder.PersistSecurityInfo;
        }

        public object GetAdvancedPropertiesObject()
        {
            return builder;
        }

        #endregion
    }

    internal class SqlCeConnectionStringBuilder : UDLStringBuilder
    {

        [Category("Connection"),DisplayName("Data Source")]
        public string DataSource { get; set; }

        [Category("Security"), PasswordPropertyText(true)]
        public string Password { get; set; }

        [Category("Security"), DefaultValue(false)]
        public bool Encrypt { get; set; }

        [Category("Security"), DefaultValue(false), DisplayName("Persist Security Info")]
        public bool PersistSecurityInfo { get; set; }

    }
}
